package com.ym.pms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.pms.dao.*;
import com.ym.pms.getCoding.NewCoding;
import com.ym.pms.model.Bunk;
import com.ym.pms.service.BunkService;
import com.ym.pms.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BunkServiceImpl implements BunkService {

    @Autowired
    BunkMapper dao;
    @Autowired
    RoomMapper rmDao;
    @Autowired
    SuiteMapper suDao;
    @Autowired
    NewCoding newCoding;
    @Autowired
    RoomService roomService;


    @Override
    public int add(Bunk bunk) {
        bunk.setBkCoding(getCoding());
        bunk.setBkRmname(rmDao.getRmName(bunk.getBkRmcoding()));
        if(rmDao.getStatus(bunk.getBkRmcoding()).equals("LOCKING")){
            bunk.setBkSouse("LOCKING");
        }else{
            bunk.setBkSouse("VACANT");
            rmDao.updateBunk("YES", bunk.getBkRmcoding());
        }
        bunk.setBkEstate(suDao.getRmEState(bunk.getBkRmcoding()));
        int insert = dao.insert(bunk);
        return insert;
    }

    @Override
    public int delete(String bkCoding) {
        if (bkCoding == null || bkCoding.equals("")) {
            return -1;
        } else if (findByCoding(bkCoding) == null) {
            return -2;
        } else {
            return dao.deleteByPrimaryKey(bkCoding);
        }
    }

    @Override
    public int update(Bunk bunk) {
        if (bunk.getBkCoding() == null || bunk.getBkCoding().equals("")) {
            return -1;
        } else if (findByCoding(bunk.getBkCoding()) == null) {
            return -2;
        } else {
            return dao.updateByPrimaryKeySelective(bunk);
        }
    }

    @Override
    public PageInfo findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Bunk> bunks = dao.queryBunkList();
        PageInfo pageInfo = new PageInfo<Bunk>(bunks);
        return pageInfo;
    }

    @Override
    public Bunk findByCoding(String bkCoding) {
        return dao.selectByPrimaryKey(bkCoding);
    }

    @Override
    public PageInfo findAllInRoom(String rmCoding, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Bunk> bunks = dao.findAllInRoom(rmCoding);
        PageInfo pageInfo = new PageInfo<Bunk>(bunks);
        return pageInfo;
    }

    @Override
    public PageInfo fuzzyQuery(Bunk bunk, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Bunk> bunks = dao.fuzzyQuery(bunk);
        PageInfo pageInfo = new PageInfo<Bunk>(bunks);
        return pageInfo;
    }

    @Override
    public void changeStatus(String bkCoding) {
        Bunk bunk = dao.selectByPrimaryKey(bkCoding);
        if(bunk.getBkSouse().equals("VACANT")){
            bunk.setBkSouse("LOCKING");
            dao.changeStatus(bunk.getBkCoding(),"LOCKING");
        }else if(bunk.getBkSouse().equals("LOCKING")){
            bunk.setBkSouse("USING");
            dao.changeStatus(bunk.getBkCoding(),"USING");
        }
        roomService.changeSatus(bunk.getBkRmcoding());
    }
    @Override
    public int getMaxNo() {
        Integer BuildingMaxNo = dao.getBuildingMaxNo();
        if (BuildingMaxNo == null) {
            return 0;
        } else {
            return BuildingMaxNo;
        }
    }

    @Override
    public String getCoding() {
        Integer maxNo = this.getMaxNo();
        maxNo += 1;
        String BuCoding = newCoding.getCoding("BK", maxNo);
        return BuCoding;
    }

    @Override
    public void deleteAllByRmCoding(String id) {
        dao.deleteAllByRmCoding(id);
    }
}
